﻿#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

#define BOARD_ROW 14
#define BOARD_COL 11

void DrawAPoint(char matrixBoard[BOARD_ROW][BOARD_COL], int headX, int headY, char style)
{
	if(headX > 0 && headX < BOARD_ROW-1 && headY > 0 && headY < BOARD_COL-1)
		matrixBoard[headX][headY] = style;
}

void DrawCar(char matrixBoard[BOARD_ROW][BOARD_COL], int headX, int headY, char style)
{	
	DrawAPoint(matrixBoard,headX,headY,style);
	DrawAPoint(matrixBoard,headX+1,headY,style);
	DrawAPoint(matrixBoard,headX+2,headY,style);
	DrawAPoint(matrixBoard,headX+3,headY,style);

	DrawAPoint(matrixBoard,headX+1,headY-1,style);
	DrawAPoint(matrixBoard,headX+1,headY+1,style);
	DrawAPoint(matrixBoard,headX+3,headY-1,style);
	DrawAPoint(matrixBoard,headX+3,headY+1,style);
}

void CreateGameBoardDemo(char matrixBoard[BOARD_ROW][BOARD_COL])
{
	int i,j;
	for (i = 0; i < BOARD_ROW; i++)
	{
		for (j = 0; j < BOARD_COL; j++)
		{			
			matrixBoard[i][j] = (i==0 || j==0 || i==BOARD_ROW-1 || j==BOARD_COL-1) ? '*' : ' ';		
		}
	}

	//draw car
	DrawCar(matrixBoard,-1,5,'%');
	DrawCar(matrixBoard,5,2,'%');
	DrawCar(matrixBoard,9,5,'@');
	DrawCar(matrixBoard,11,8,'%');
}

void DisplayGameBoard(char matrixBoard[BOARD_ROW][BOARD_COL])
{
	int i,j;
	for (i = 0; i < BOARD_ROW; i++)
	{
		for (j = 0; j < BOARD_COL; j++)
		{
			fflush(stdout);
			printf("%c", matrixBoard[i][j]);
		}
		printf("\n");
	}
}

int main()
{
	char matrixBoard[BOARD_ROW][BOARD_COL];
	char c;

	printf("WELCOME TO CAR RACING GAME!!!\n");
	printf("=============================\n");
	printf("Press any key to start...\n\n");
	getch();

	printf("Life: $$$$$\t Scores: 69\n");

	CreateGameBoardDemo(matrixBoard);
	DisplayGameBoard(matrixBoard);

	printf("Press X to exit, press R to restart\n");

	//getch();
	//system("cls");
	//_sleep(5000);
	
	c = getch();
	
	//printf("%x",c);
	//printf("%c",getch());

	if(c==-32) //don't know why, it is result of test
	{
		c= getch();
		if(c==75)
		{
			printf("You pressed Left arrow");		
		}
		else if(c==77)
		{
			printf("You pressed Right arrow");			
		}
	}
	else if(c==88 || c==120)
	{
		printf("You pressed x key");
	}
	getch();

	return 0;
}